﻿myApp.controller('LoginController', function ($scope, authService, $http, $location, $rootScope, $facebook, $http, localStorageService,$window) {
    $scope.title = "HELLO WORLD!";
    $scope.login = function () {
        console.log("logining..!");
        

        if (!$scope.loginData || !$scope.loginData.userName || !$scope.loginData.password) {
            $scope.message = "Invalid username or password!";
            return;
        }

        console.log("Login Data: " + JSON.stringify($scope.loginData));
        
        authService.login($scope.loginData).then(function (response) {
            $rootScope.isAdmin = false;

            $scope.user = $scope.loginData;
           
            //console.log("RESPONSE: " + JSON.stringify($scope.authentication));

            if (response.UserType == "Valid User") {
                $location.path('/');

                $window.location.reload();
                console.log("Valid User!");
               // alert("Valid User");
            }


            
            if ($scope.loginData.userName.indexOf('@') === -1) {
                $rootScope.isAdmin = true;
                console.log("Listing Entry!");
                $rootScope.$broadcast('userLoggedIn');

                $location.path('/');
                //alert("Listing Entry")

            } else {
                console.log("Go to News!");
                //$location.path('/News');
            }
            setTimeout(function () {

                $window.location.reload();
            }, 1000);


        },
        function (err) {
           // alert(JSON.stringify(err));
            $scope.message = "Invalid username or password!";
        })
    }

    $scope.isLoggedIn = false;
    $scope.FBLogin = function () {
        //alert("FB LOGIN...");
        $facebook.login().then(function () {

           refresh();
        });
    }

    $scope.FBLogout = function () {
        $facebook.logout().then(function () {
            //alert("Logout!");
        });
    }

    function refresh() {
        $facebook.api("/me").then(
            function (response) {
                $scope.isLoggedIn = true;

                var data = "grant_type=password&username=" +
                response.id + "&password=";

                $http.post('/api/authorization/Token', data, {
                    headers:
                    { 'Content-Type': 'application/x-www-form-urlencoded' }
                }).success(function (r) {
                    console.log("SUCCESS!: " + JSON.stringify(r));
                    localStorageService.set('authorizationData',
                    { token: r.access_token, userName: response.name, userType: r.UserType, isFacebookUser: true });
                    $location.path('/');
                    $window.location.reload();

                }).error(function(error){
                    $location.path('/fb/register');
                });


                //$http.post('/api/authorization/Token', { grant_type: 'password', username: response.id, password: '' }).then(function (data) {
                //    alert("RESPONSE #2: " + JSON.stringify(data));
                //});
                    

                $scope.welcomeMsg = "Welcome " + response.name;
            },
            function (err) {
              //  alert("ERR RESPONSE: " + JSON.stringify(err));

                $scope.welcomeMsg = "Please log in";
            });
    }


});